package com.mpyy.handler.sales;

import com.alibaba.fastjson.JSON;
import com.mpyy.pojo.DepotInfo;
import com.mpyy.pojo.InventoryDesc;
import com.mpyy.pojo.ProductInfo;
import com.mpyy.service.sales.ReturnedPurchaseServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * @Description ReturnedPurchaseDaoInter
 * @Author YYJC
 * @Date 2022-03-07 9:03
 */
@RestController
public class ReturnedPurchaseController {
    @Autowired
    @Qualifier(value = "returnedPurchaseService")
    private ReturnedPurchaseServiceInter returnedPurchaseServiceInter;

    /**
     * 查询商品清单
     * @return
     */
    @RequestMapping(value = {"/showReturnedPurchase"})
    public String searchReturnedPurchase(ProductInfo productInfo){
        List<ProductInfo> productInfos = returnedPurchaseServiceInter.searchReturnedPurchase (productInfo);

        System.out.println (productInfos);
        HashMap<String,Object> productInfoMap = new HashMap<> ();
        productInfoMap.put ("code",0);
        productInfoMap.put ("msg","");
        productInfoMap.put ("count",productInfos.size ());
        productInfoMap.put ("data",productInfos);

        return JSON.toJSONString (productInfoMap);
    }

    /**
     * 仓库下拉框
     * @param response
     * @param request
     * @throws IOException
     */
    @RequestMapping(value = "/showStorehouse")
    public void searchStorehouse(HttpServletResponse response, HttpServletRequest request) throws IOException {
        List<DepotInfo> depotInfos = returnedPurchaseServiceInter.searchStorehouse();

        request.setCharacterEncoding("utf-8");  //防止乱码
        response.setContentType("text/html;charset=utf-8");
        response.setHeader("Cache-Control", "no-cache");

        /*开启流*/
        PrintWriter writer = response.getWriter ();
        /*改为json格式*/
        writer.print (JSON.toJSONString (depotInfos));
        /*关闭流*/
        writer.close ();

    }

    /**
     * 查询商品批次信息
     * @param proId
     * @return
     */
    @RequestMapping(value = "/showCommodityBatch/{proId}")
    public String searchCommodityBatch(@PathVariable("proId") String proId){
        InventoryDesc inventoryDesc = new InventoryDesc();
        inventoryDesc.setProId (proId);
        List<InventoryDesc> inventoryDescs = returnedPurchaseServiceInter.searchCommodityBatch (inventoryDesc);

        HashMap<String,Object> inventoryDescMap = new HashMap<> ();
        inventoryDescMap.put ("code",0);
        inventoryDescMap.put ("msg","");
        inventoryDescMap.put ("count",inventoryDescs.size ());
        inventoryDescMap.put ("data",inventoryDescs);
        return JSON.toJSONString (inventoryDescMap);
    }



}